﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF.Model;
using WF.DAL;
using WF.Common;

namespace WF.BusinessObject
{
    public class Role : BOBase<RoleEntity, DAL.Role>
    {
        #region 构造函数

        public Role() : base() { }
        public Role(WFDBDataContext DataContext)
            : base(DataContext)
        {

        }
        #endregion

        #region 实现基类abstract方法
        /// <summary>
        /// 实现基类的GetById abstract方法
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public override RoleEntity GetById(string ID)
        {
            var obj = DataContext.Roles.SingleOrDefault(o => o.RoleID == ID);
            return new RoleEntity(obj);
        }
        #endregion

        public void MarkDeletedFlag(string ID, FlagDeleted deletedFlag)
        {
            RoleEntity _roleEntity = GetById(ID);
            DataContext.RoleAuthOperates.DeleteAllOnSubmit(_roleEntity.DBEntity.RoleAuthOperates);
            _roleEntity.FlagDeleted = (short)deletedFlag;
            DataContext.SubmitChanges();
        }

        public void MarkDeletedFlag(RoleEntity roleEntity, FlagDeleted deletedFlag)
        {
            RoleEntity _roleEntity = this.GetById(roleEntity.ID);
            DataContext.RoleAuthOperates.DeleteAllOnSubmit(_roleEntity.DBEntity.RoleAuthOperates);
            _roleEntity.FlagDeleted = (short)deletedFlag;
            DataContext.SubmitChanges();
        }

        public List<string> GetAllModule()
        {
            var objs = from a in DataContext.Operates where a.FlagDeleted == 0 orderby a.OperateNo group a by a.ModelName into b select b.Key;
            return objs.ToList();
        }

        public List<RoleAuthOperateEntity> GetRoleAuthOperate(string _roleID)
        {
            RoleEntity role = this.GetById(_roleID);
            if (null == role)
            {
                return new List<RoleAuthOperateEntity>();
            }
            else
            {
                var objs = from a in role.DBEntity.RoleAuthOperates where a.Operate.FlagDeleted == 0 select new RoleAuthOperateEntity(a);
                return objs.ToList();
            }
        }

        public List<ModuleOpearateAuthItem> GetRoleAuthOperate(string _roleID, string _moduleName)
        {
            var objs = from a in DataContext.Operates where a.ModelName == _moduleName && a.FlagDeleted == 0 group a by a.ModelName into b select new { ModuleName = b.Key, Operates = b };
            List<ModuleOpearateAuthItem> ret = new List<ModuleOpearateAuthItem>();
            foreach (var module in objs)
            {
                foreach (var operate in module.Operates)
                {
                    AuthDeptRange authDeptRange = GetOperAteAuth(_roleID, operate.OperateID);
                    ModuleOpearateAuthItem obj = new ModuleOpearateAuthItem();
                    obj.ModuleName = module.ModuleName;
                    obj.OperateID = operate.OperateID;
                    obj.OperateName = operate.OperateName;
                    obj.IsAuth = authDeptRange == AuthDeptRange.无 ? false : true;
                    obj.DeptRelate = operate.DeptRelate;
                    obj.DeptRange = authDeptRange;
                    ret.Add(obj);
                }
            }
            return ret;

        }

        public bool IsOperateAuth(string _roleID, string _operateID)
        {
            int count = DataContext.RoleAuthOperates.Where(o => (o.RoleID == _roleID && o.OperateID == _operateID)).Count();
            return (count > 0);
        }

        public AuthDeptRange GetOperAteAuth(string _roleID, string _operateID)
        {
            var objs = from a in DataContext.RoleAuthOperates where (a.RoleID == _roleID && a.OperateID == _operateID) select a;
            if (objs.Count() == 0)
            {
                return AuthDeptRange.无;
            }
            else
            {
                return (AuthDeptRange)objs.First().DeptRange;
            }
        }


        public void RoleAuthOperate(string roleID, List<string> operateIDs)
        {
            RoleEntity role = this.GetById(roleID);
            if (null != role)
            {
                RoleAuthOperate roleAuthOperateBO = new RoleAuthOperate();

                DataContext.RoleAuthOperates.DeleteAllOnSubmit(role.DBEntity.RoleAuthOperates);

                foreach (string operateID in operateIDs)
                {
                    DAL.RoleAuthOperate o = new DAL.RoleAuthOperate();
                    o.RoleID = roleID;
                    o.OperateID = operateID;
                    o.DeptRange = (short)AuthDeptRange.所有;
                    o.ID = Guid.NewGuid().ToString();
                    DataContext.RoleAuthOperates.InsertOnSubmit(o);
                }
                DataContext.SubmitChanges();
            }
        }

        public void RoleAuthOperate(string roleID, List<ModuleOpearateAuthItem> opearateAuthItems)
        {
            RoleEntity role = this.GetById(roleID);
            if (null != role)
            {
                RoleAuthOperate roleAuthOperateBO = new RoleAuthOperate();

                DataContext.RoleAuthOperates.DeleteAllOnSubmit(role.DBEntity.RoleAuthOperates);

                foreach (ModuleOpearateAuthItem item in opearateAuthItems)
                {
                    if (item.DeptRange >= AuthDeptRange.本人)
                    {
                        DAL.RoleAuthOperate o = new DAL.RoleAuthOperate();
                        o.RoleID = roleID;
                        o.OperateID = item.OperateID;
                        o.DeptRange = (short)item.DeptRange;
                        o.ID = Guid.NewGuid().ToString();
                        DataContext.RoleAuthOperates.InsertOnSubmit(o);
                    }
                }
                DataContext.SubmitChanges();
            }
        }

        /*

        public List<EmployeeToRoleEntity> GetEmployeeToRole(string roleID)
        {
            RoleEntity roleEntity = this.GetById(roleID);
            if (null == roleEntity)
            {
                return new List<EmployeeToRoleEntity>();
            }
            else
            {
                Role1 roleDBEntity = roleEntity.DBEntity;
                var objs = from a in roleDBEntity.EmployeeToRoles orderby a.Employee.EmpNo select (new EmployeeToRoleEntity(a));

                return objs.ToList();
            }
        }

        public void DeleteEmployeeToRole(EmployeeToRoleEntity employeeToRoleEntity)
        {
            EmployeeToRole employeeToRoleBO = new EmployeeToRole(DataContext);
            employeeToRoleBO.Delete(employeeToRoleEntity);    
        }

        public List<PositionEntity> GetPositionToRole(string roleID)
        {
            RoleEntity roleEntity = this.GetById(roleID);
            if (null == roleEntity)
            {
                return new List<PositionEntity>();
            }
            else
            {
                Role1 roleDBEntity = roleEntity.DBEntity;
                var objs = from a in roleDBEntity.Positions orderby a.Path select (new PositionEntity(a));
                return objs.ToList();
            }
        }

        public void DeletePositionToRole(PositionEntity positionEntity)
        {
            Position position = new Position(DataContext);
            positionEntity = position.GetById(positionEntity.ID);
            positionEntity.RoleID = SecurityManager.CommonUserRoleID;
            position.Update(positionEntity);
        }
        */
    }
}